# 2.1 符号和常量 ## 一、符号 Verilog 语言规定了一套完整的语法结构。 程序由符号流构成 , 符号包括间隔符、注释符、操作符、数字、字符串、标识符、关键字等等。 1. **间隔符**(White Space):包括空格、Tab、换行、换页等。空白符能够使代码便于阅读,且在综合时空白符会被忽略。 2. **注释符**(Comments):改善程序的可读性,在编译时不起作用。 1. 单行注释:以`//`开始到本行结束; 2. 多行注释:多行注释以`/*`开始,到`*/`结束。 3. **标识符**(Identifiers):给对象(如模块、输入/输出端口、变量等)取的名字。可以是任意一组字母、数字以及符号`$`和`_`(下划线)的组合,但标识符的第一个字符必须是字母或者下划线,且大小写敏感。如:`clk`、`counter8`、`_net`、`bus_A` 4. **关键字**(Keywords):或称保留字,指已被Verilog内部占用的字,不能作为变量或节点名字使用。关键字都是小写的。 ```verilog always, and, assign, begin, buf, bufif0, bufif1, case, casex, casez, cmos, deassign, default, defparam, disable, edge, else, end, endcase, endmodule, endfunction, endprimitive, endspecify, endtable, endtask, event, for, force, forever, fork, function, highz0, highz1, if, initial, inout, input, integer, join, large, macromodule, medium, module, nand, negedge, nmos, nor, not, notif0, notifl, or, output, parameter, pmos, posedge, primitive, pull0, pull1, pullup, pulldown, rcmos, reg, releses, repeat, mmos, rpmos, rtran, rtranif0, rtranif1, scalared, small, specify, specparam, strength, strong0, strong1, supply0, supply1, table, task, time, tran, tranif0, tranif1, tri, tri0, tri1, triand, trior, trireg, vectored, wait, wand, weak0, weak1, while, wire, wor, xnor, xor ``` ## 二、逻辑值 Verilog 采用4值逻辑:0、1、X、Z(X、Z不区分大小写)。Verilog 中的所有数据类型都在上述4类逻辑状态中取值。 | 逻辑值 | 说明 | |------|----------------------| | 0 | 逻辑0、逻辑假 | | 1 | 逻辑1、逻辑真 | | X(x) | 不确定的值(未知态) | | Z(z) | 高阻态 | ## 三、常量及其表示 在Verilog语言中,用参数`parameter`来定义一个标识符代表一个常量,常用来定义时延和变量的宽度。 ```verilog // 格式 parameter parameter1 = expression1, parameter2 = expression2, ...; // 举例 parameter SEL=8, CODE=8'ha3; ``` 1. 整数型 1. 带基数形式的表示方法:`<+/-><位宽>'<基数符号><数值>` 1. 位宽:对应二进制数的位宽 2. 基数符号(表示进制,不区分大小写):二进制B、十进制D或缺省、八进制O、十六进制H 3. 数值:基于进制的数字序列 4. 举例:`3'b101`、`5'o37`、`8'he3`、`8'b1001_0011`、`4'B1x_01`、`5'Hx`(注意:下划线 `_` 表示分位标记符) 2. 十进制数形式的表示方法(有符号):`30`、`-2` 2. 实数型 1. 十进制计数法:`0.1`、`2.0`、`5.67` 注意:小数点两侧都必须有数字,`.2`、`2.` 是错误写法。 2. 科学计数法:`23_5.1e2`(=23510.0)、`5E-4`(=0.0005) 注意:E、e不做区分 3. 字符串 1. 字符串是双引号内的字符序列,其中一个字符占8位:`hello`、`Verilog` 2. 字符串不能分为多行书写 3. 字符串的作用主要是用于仿真时,显示一些相关的信息,或者指定显示的格式